package com.example.mytestdemo.viewpage2.adapter

import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.mytestdemo.R
import com.example.mytestdemo.databinding.ActivityViewpager2ListadapterItemLayoutBinding
import com.example.mytestdemo.BR

/**
 * @author tgw
 * @date 2022/7/5
 * @describe
 */
class MyViewPager2DiffutilAdapter : RecyclerView.Adapter<MyViewPager2DiffutilAdapter.PagerViewHolder>() {

    companion object {
        private const val TAG = "MyViewPager2Adapter"
    }


    private lateinit var mBind: ActivityViewpager2ListadapterItemLayoutBinding
    private var mList: List<String> = ArrayList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagerViewHolder {
        mBind = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.activity_viewpager2_listadapter_item_layout,
            parent,
            false
        )

        Log.d(TAG, "onCreateViewHolder: ")
        return PagerViewHolder(mBind)
    }

    override fun onBindViewHolder(holder: PagerViewHolder, position: Int) {
        var itemData = mDiffer.currentList[position]
        holder.bindData(itemData,position)

        //这种xml绑定的方式
//        holder.mBind.setVariable(BR.item, itemData);
//        holder.mBind.executePendingBindings();
        //xml  需配置
        // <variable
        //            name="item"
        //            type="String" />
        //
        //          文本控件  android:text="@{item}"




        Log.d(TAG, "onBindViewHolder: $position")
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        Log.d(TAG, "onAttachedToRecyclerView: ")
    }

    override fun onViewAttachedToWindow(holder: PagerViewHolder) {
        super.onViewAttachedToWindow(holder)
        Log.d(TAG, "onViewAttachedToWindow: ${holder.adapterPosition}")
    }

    override fun onViewDetachedFromWindow(holder: PagerViewHolder) {
        super.onViewDetachedFromWindow(holder)
        Log.d(TAG, "onViewDetachedFromWindow: ${holder.adapterPosition}")
    }

    override fun onBindViewHolder(
        holder: PagerViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        super.onBindViewHolder(holder, position, payloads)
        Log.d(TAG, "onBindViewHolder: payloads $position")
    }

    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
        Log.d(TAG, "onDetachedFromRecyclerView: ")
    }

    override fun onViewRecycled(holder: PagerViewHolder) {
        super.onViewRecycled(holder)
        Log.d(TAG, "onViewRecycled: ${holder.adapterPosition}")
    }




    override fun getItemCount(): Int {
        return mDiffer.currentList.size
    }

    //2. 创建 AsyncListDiff 对象
    private val mDiffer = AsyncListDiffer<String>(this, DataDiffCallback())



    fun submitList(studentList: List<String>) {
        //3. 提交新数据列表
        mDiffer.submitList(studentList)
    }




    //	ViewHolder需要继承RecycleView.ViewHolder
    class PagerViewHolder(val mBind: ActivityViewpager2ListadapterItemLayoutBinding) :
        RecyclerView.ViewHolder(mBind.root) {

        private var colors = arrayOf("#CCFF99", "#41F1E5", "#8D41F1", "#FF99CC")

        fun bindData(data: String,position: Int) {
            mBind.tv.text = "我是viewpager2- -DiffCallback的布局-----：$data"
            mBind.tv.setBackgroundColor(Color.parseColor(colors[position % 4]))
        }
    }


    class DataDiffCallback : DiffUtil.ItemCallback<String>() {

        override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem != newItem
        }


    }


}

